package com.ptit.piclient;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.ptit.piclient.model.Album;
import com.ptit.piclient.model.Song;
import com.ptit.piclient.tools.AllSongAdapter2;
import com.ptit.piclient.tools.PiConnector;
import com.ptit.piclient.tools.Utils;

public class SA_Album_Details extends Activity implements OnItemClickListener {
	ListView lv;
	ArrayList<Song> list = new ArrayList<Song>();
	//Oct 8
	Album currentAlbum;int albumID = 0;
	
	public Album getCurrentArtist() {
		return currentAlbum;
	}

	public void setCurrentArtist(Album currentAlbum) {
		this.currentAlbum = currentAlbum;
	}

	// End Oct 8
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sb_artist_details);
		lv = (ListView) findViewById(R.id.sb_artist_allSong);
		lv.setOnItemClickListener(this);
		albumID = getIntent().getIntExtra("albumid", 0);
		//Oct 11
		String albumName = getIntent().getStringExtra("albumname");
		if(albumName != null && albumName.equals("") == false)this.setTitle(albumName);
		//End Oct 11
		new UpdateView(this).execute("");
	}
	
//	private class AudioLibraryScan extends AsyncTask<Void, Void, Void> {
//		private Activity act;
//
//		public AudioLibraryScan(Activity a) {
//			act = a;
//		}
//
//		@Override
//		protected Void doInBackground(Void... arg0) {
//			PiConnector.getInstance(act).audioLibraryScan();
//			new UpdateView(act).execute();
//			return null;
//		}
//
//		@Override
//		protected void onProgressUpdate(Void... params) {
//			ProgressDialog dialog = new ProgressDialog(SA_Album_Details.this);// ,
//																				// "",
//																				// "Please wait...",true);
//			dialog.setTitle("");
//			dialog.setMessage("Please wait...");
//			dialog.setIndeterminate(true);
//			dialog.show();
//
//		}
//
//		// khuongdv start sep28
//		@Override
//		protected void onPostExecute(Void result) {
//
//		}
//		// khuongdv end sep28
//	}

	/*
	 * New thread to update listview
	 */
	private class UpdateView extends AsyncTask<String, Void, AllSongAdapter2> {
		Activity act;
		ProgressDialog dialog;

		public UpdateView(Activity a) {
			act = a;
			currentAlbum = new Album();
			currentAlbum.setAlbumid(albumID);
			dialog = new ProgressDialog(SA_Album_Details.this);
			dialog.setTitle("");
			dialog.setMessage("Please wait...");
			dialog.setIndeterminate(true);
			dialog.show();
		}

		@Override
		protected AllSongAdapter2 doInBackground(String... params) {
			
			PiConnector.getInstance(act).addSongOfAlbumToPlaylist(albumID);
			list = PiConnector.getInstance(act).getSongsOfAlbumByKeyword(currentAlbum,params[0]);// getAllSongsFromPI();
			AllSongAdapter2 ad = new AllSongAdapter2(act, list);
			return ad;
		}

		@Override
		protected void onPreExecute() {}

		@Override
		protected void onProgressUpdate(Void... params) {}

		protected void onPostExecute(AllSongAdapter2 result) {
			dialog.dismiss();
			if (result != null)
				lv.setAdapter(result);
		}
	}

	/**
	 * Play a song that you 've clicked on it
	 * 
	 * @author Khuong
	 * 
	 */
	private class PlaySong extends AsyncTask<Integer, Void, Void> {
		Activity act;
		int position;
		Song s;
		public PlaySong(Activity a,int position, Song s) {
			act = a;
			this.s = s;
			this.position = position;
		}

		@Override
		protected Void doInBackground(Integer... params) {
			if (s != null) {
				try {
					PiConnector.getInstance(act).playSongInListSongOfArtistOrAlbum(position);
				} catch (Exception ex) {
					ex.printStackTrace();
				}
				Log.i("nowplaying", "Moving...");
			}
			return null;
		}

		@Override
		protected void onPreExecute() {

		}

		protected void onPostExecute(Void result) {
			Intent intent = new Intent(act, NowPlayingActivity.class);
			intent.putExtra("currentsong", Utils.serialize(s));
			startActivity(intent);
		}

	}

	// =============END ASYNTASK
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		new PlaySong(this, position, (Song)list.get(position)).execute();
	}

}